//
//  YSGNurseAppointmentOrderCell.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/3/17.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
import Kingfisher

protocol YSGNurseAppointmentOrderCellDelegate: NSObjectProtocol {
    func cancel(_ orderId: String ,_ model :YSGNurseAppointmentOrderModel)
    func evaluate(_ appointmentId: String)
    func pay(_orderId: String ,_appointmentId : String)
    func nurseCancel(_ diagnoseId : String)
}


class YSGNurseAppointmentOrderCell: UITableViewCell {

    weak var delegate: YSGNurseAppointmentOrderCellDelegate?
    
    var model :YSGNurseAppointmentOrderModel?
    var nurseModel :YSGNurseFindPatientOrderModel?
    @IBOutlet weak var payInfoView: UIView!
    @IBOutlet weak var statusLabel: UILabel!
    @IBOutlet weak var timeLimitedLabel: UILabel!
    @IBOutlet weak var priceLabel: UILabel!
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var addressLabel: UILabel!
    @IBOutlet weak var payButton: UIButton!
    @IBOutlet weak var nameLabel: UILabel!
    
    @IBOutlet weak var nurseHeadImageView: UIImageView!
    
    @IBOutlet weak var nurseNameLabel: UILabel!
    
    @IBOutlet weak var cancelButton: UIButton!{
        didSet {
            cancelButton.layer.borderColor = UIColor.init(hex: "#4E7DFB").cgColor
            cancelButton.layer.borderWidth = 1
        }
    }
    
    @IBOutlet weak var reasonLabel: UILabel!
    
    
    @IBOutlet weak var cancelButtonRightLayout: NSLayoutConstraint!

    
    @IBAction func cancelButtonAction(_ sender: UIButton) {
        
        if (nurseModel != nil) {
            switch nurseModel?.status{
            case .finish:
                delegate?.evaluate(nurseModel?.orderId ?? "")
            default:
                delegate?.nurseCancel(nurseModel?.diagnoseId ?? "")
            }
            return
        }
        
        switch model?.appointmentStatus{
        case .completed:
            delegate?.evaluate(model?.appointmentId ?? "")
        default:
            delegate?.cancel(model?.orderId ?? "",model!)
        }
    }
    
    @IBAction func payButtonAction(_ sender: UIButton) {
        delegate?.pay(_orderId: model?.orderId ?? "", _appointmentId: model?.appointmentId ?? "")
        
    }
    
    func nurseConfig(_ model: YSGNurseFindPatientOrderModel)
    {
        nurseModel = model
        statusLabel.attributedText = NSMutableAttributedString(
            string: model.status?.title ?? "",
            attributes: [NSAttributedString.Key.font:
                            UIFont(name: "PingFangSC-Semibold", size: 14.0)!,NSAttributedString.Key.foregroundColor:model.status?.titleColor])
        
        nameLabel.text = "预约人：" + (model.patientName ?? "")
        addressLabel.text = "预约时间：" + (model.orderTime ?? "")

        
        nurseNameLabel.attributedText =  NSMutableAttributedString(
            string: model.nurseName ?? "暂未分配护士，请等待",
            attributes: [NSAttributedString.Key.font:
                UIFont(name: "PingFangSC-Semibold", size: 14.0)!])
        
        nurseHeadImageView.kf.setImage(with: URL(string: model.nurseAvatar ?? ""),
                               placeholder: UIImage(named: "profile_headimg"))
        
        timeLabel.font = UIFont.systemFont(ofSize: 14)
        
        cancelButton.setTitle("取消订单", for: .normal)
        timeLabel.text = "";
        payInfoView.isHidden = true
        cancelButton.isHidden = true
        reasonLabel.isHidden = true
        cancelButtonRightLayout.constant = 20
        payButton.isHidden = true
        if !model.finishTime!.isEmpty{
            timeLabel.text = "结束时间：" + (model.finishTime ?? "")
        }else{
            timeLabel.text = ""
        }
        
        switch model.status {
        case .waitAccpet,.unpaid:
            cancelButton.isHidden = false
        case .finish:
            cancelButton.isHidden = false
            cancelButton.setTitle("立即评价", for: .normal)
        default: break
            
        }
        
    }
    
    func config(_ model: YSGNurseAppointmentOrderModel)
    {
        self.model = model
        statusLabel.attributedText = NSMutableAttributedString(
            string: model.appointmentStatus?.title ?? "",
            attributes: [NSAttributedString.Key.font:
                            UIFont(name: "PingFangSC-Semibold", size: 14.0)!,NSAttributedString.Key.foregroundColor:model.appointmentStatus?.titleColor])
        nameLabel.text = "预约人：" + model.patientName
        addressLabel.text = "地址：" + model.adress
        timeLabel.text = "预约时间：" + (model.appointmentTime ?? "")
        payInfoView.isHidden = model.appointmentStatus?.rowheight == 227
        nurseHeadImageView.image = UIImage(named: "profile_headimg")
        nurseNameLabel.attributedText =  NSMutableAttributedString(
            string: "暂未分配护士，请等待",
            attributes: [NSAttributedString.Key.font:
                UIFont(name: "PingFangSC-Semibold", size: 14.0)!])
        
        
        let leftMinString = NSMutableAttributedString(
            string: "请在" + (model.leftMin ?? "") + "分钟内完成支付",
            attributes: [NSAttributedString.Key.font:
                UIFont(name: "PingFang SC", size: 12.0)!])
        leftMinString.addAttribute(NSAttributedString.Key.foregroundColor,value:UIColor.warning, range: NSRange(location: 2,length: model.leftMin?.count  ?? 0))
        timeLimitedLabel?.attributedText = leftMinString
        
        reasonLabel.isHidden = true
        
        for item in model.consumeMaterials {
            if item.orderStatus == .unpaid{
                reasonLabel.isHidden = false
                reasonLabel.text = "<有额外耗材费用等待支付，点击查看>"
                reasonLabel.textColor = UIColor.init(hex: "#F09062")
            }
        }
        
        cancelButton.setTitle("取消订单", for: .normal)
        switch model.appointmentStatus?.buttonStatus {
        case .all:
            cancelButton.isHidden = false
            payButton.isHidden = false
            cancelButtonRightLayout.constant = 102
        case .cancel:
            payButton.isHidden = true
            cancelButton.isHidden = false
            cancelButtonRightLayout.constant = 20
        case .completed:
            payButton.isHidden = true
            cancelButton.isHidden = false
            cancelButtonRightLayout.constant = 20
            cancelButton.setTitle("立即评价", for: .normal)
        case .evaluated:
            cancelButton.isHidden = true
            payButton.isHidden = true
        default:
            break
        }
        
        
        

        
        
        
        switch model.appointmentStatus {
        case .unpaid:
            payInfoView.isHidden = false
            priceLabel.text = "￥" + String(format: "%.2f", (model.orderAmount?.floatValue ?? 0)/100.0)
 
        case .notApproved_unassigned,.notApproved_missedOrders:
            payInfoView.isHidden = true
            
        case .reserved,.completed,.evaluated:
            payInfoView.isHidden = true
            nurseHeadImageView.image = UIImage(named: "profile_headimg_nurse")
    
            nurseNameLabel.attributedText = YSGNurseAppointmentOrderModel.getNurseName(model.nurseName ?? "")

        case .cancel:
            reasonLabel.isHidden = false
            reasonLabel.text = "取消原因：" + model.cancelReason
            reasonLabel.textColor = UIColor.textGray
            payInfoView.isHidden = true
            nurseNameLabel.text = ""
        default:
            payInfoView.isHidden = true
        }
        
        
        switch model.orderStatus {
        case .refund,.error:
            cancelButton.isHidden = true
            statusLabel.text = model.orderStatus?.title
        default:
            break
        }
        
    
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
}
